﻿using System;
using System.Linq;
using System.ServiceModel.Activation;
using GraphMyCode.Web.Dtos;

namespace GraphMyCode.Web
{
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	public class AssemblyService : IAssemblyService
	{

		#region IAssemblyService Members

		public GraphMapDto GetGraph(byte[] assemblyBytes, double canvasWidth, double canvasHeight, ExportType exportType)
		{
			GraphMapDto graphMap;
			switch (exportType)
			{
				case ExportType.Assembly:
					graphMap = AssemblyReader.GetAssemblyGraph(assemblyBytes);
					break;
				case ExportType.Class:
					graphMap = AssemblyReader.GetClassGraph(assemblyBytes);
					break;
				case ExportType.Method:
					graphMap = AssemblyReader.GetMethodGraph(assemblyBytes);
					break;
				default:
					throw new ArgumentOutOfRangeException("exportType");
			}

			double coef = (double)graphMap.Graph.Vertices.Count() / 100;
			if(coef > 1)
			{
				canvasWidth *= coef;
				canvasHeight *= coef;
			}
			
			LayoutService.Run("random", graphMap.Graph, canvasWidth, canvasHeight);
			return graphMap;
		}

		#endregion
	}
}
